﻿define(function (require, exports, module) {
    var CPDF_TextObject = require('../../PDFData/Text/Reader_TextObject.js');
    var CPDF_TextPage = require('../../PDFData/Text/Reader_TextPage.js');
    var CReader_TextPageSelect = require('./Reader_TextPageSelect.js');
    var CPDF_Point = FXREADER.CPDF_Point;
    var CPDF_Rect = FXREADER.CPDF_Rect;
    var CommonTools = FXREADER.Common;
    var CARET_INFO = FXREADER.PDFData.Text.CARET_INFO;
    var BOUND_INFO = FXREADER.PDFData.Text.BOUND_INFO;

    var SELECT_STATE = {
        SELECT_MODE_NORMAL: 0,
        SELECT_MODE_IN_SELSETED: 1,
        SELECT_MODE_SELECTING: 2,
        SELECT_MODE_BOUND: 3
    };
    FXREADER.TextSelect.SELECT_STATE = SELECT_STATE;

    var CURSOR_STATE = {
        CURSOR_NORMAL: 0,
        CURSOR_SELECT: 1,
        CURSOR_ARROW: 2,
        CURSOR_BOUND: 3,
        CURSOR_QKSCH: 4
    };
    FXREADER.TextSelect.CURSOR_STATE = CURSOR_STATE;

    /**
    * @description 文本页面类
    * @class 文本页面类
    * @ignore
    * @constructor
    */
    FXREADER.TextSelect.TEXT_PAGE = function () {
        /**
        * @description PDF文本页面对象
        * @public
        * @field
        * @type {FXREADER.PDFData.Text.CPDF_TextPage}
        */
        this.pTextPage = null;
        /**
        * @description 文本页面的选择文本信息对象
        * @public
        * @field
        * @type {FXREADER.TextSelect.CReader_TextPageSelect}
        */
        this.pageSelectInfo = new CReader_TextPageSelect();
    };
    var TEXT_PAGE = FXREADER.TextSelect.TEXT_PAGE;

    /**
    * @description 页面文本选择区域类
    * @class 页面文本选择区域类
    * @ignore
    * @constructor
    */
    FXREADER.TextSelect.TEXT_SELECTED_INFO = function () {
        /**
        * @description 页面索引(从0开始)
        * @public
        * @field
        * @type {Number}
        */
        this.pageIndex = -1;
        /**
        * @description 页面文本选择区域数组
        * @public
        * @field
        * @type {Array.<{FXREADER.CPDF_Rect}>}
        */
        this.selectedRectArray = [];
    };

    /**
    * @description Text selection tool
    * @class Text selection tool class
    * @ignore
    * @constructor
    * @param {FXREADER.CReaderApp} pReaderApp App instance
    */
    FXREADER.TextSelect.CReader_TextSelectTool = function (pReaderApp) {
        var _pReaderApp = pReaderApp,
	        _pCurPageView = null,
	        _pTextPageList = [],
	        _pSLArray = [], //有文本被选择的文本页面索引数组，格式： 文本页面索引
	        _bSearch = false,
	        _bSLBound = false,
             _startwindowpoint = new CPDF_Point(0, 0),
             _endwindowpoint = new CPDF_Point(0, 0),
             _nStartPageIndex = -1,
             _select_mode = SELECT_STATE.SELECT_MODE_NORMAL,
             _cursorState = CURSOR_STATE.CURSOR_SELECT, //鼠标样式
             _bLButtomDown = false,
             _PageHighlightCreatedList = {},  // 存储已经创建highlight div的页面索引,格式为 :   页面索引：true/fase;
              _dLastTime = 0,
              _boundSelectionRectDiv = null, //标记范围选择区域Div元素jQuery对象
              _jqSelectionOperationBarDiv = null, //选完文本后操作Div元素jQuery对象
              _curCursorStyleName = null,
              _self = this;
        var _CurCaretInfo = new CARET_INFO();
        var _StartCaretInfo = new CARET_INFO();
        var _beginSelectionPoint = new CPDF_Point(0, 0),
             _endSelectionPoint = new CPDF_Point(0, 0);

        /**
         * @ignore
        * @description 获取标记范围选择区域的Div元素jQuery对象
        * @private
        * @return {Object} 标记范围选择区域元素jQuery对象;
        */
        function _GetBoundSelectionRectDiv() {
            if (_boundSelectionRectDiv == null) {
                var pDocView = _pReaderApp.GetMainView().GetDocView();
                var pFirstPageView = pDocView.GetPageView(0);
                var jqPagesContainerDiv = $("#" + pDocView.GetDocViewContainerID());
                var selectionDivID = pDocView.GetDocViewContainerID() + "_text_bound_select";
                var divHtml = "<div class='fwr_text_bound_selection fwr_hidden' id='" + selectionDivID + "'></div>";
                if (pFirstPageView != null && pFirstPageView.IsContentCreated()) {
                    $("#" + pFirstPageView.GetPageContainerID()).parent().parent().append(divHtml);
                }
                else {
                    jqPagesContainerDiv.append(divHtml);
                }
                _boundSelectionRectDiv = $("#" + selectionDivID);
            }
            return _boundSelectionRectDiv;
        }
        /**
         * @ignore
        * @description 获取选完文本后操作的Div元素jQuery对象
        * @private
        * @return {Object} 选完文本后操作元素jQuery对象;
        */
        function _GetSelectionOperationDiv() {
            if (_jqSelectionOperationBarDiv == null) {
                var pDocView = _pReaderApp.GetMainView().GetDocView();
                var pFirstPageView = pDocView.GetPageView(0);
                var jqPagesContainerDiv = $("#" + pDocView.GetDocViewContainerID());
                var selectionOperationDivID = pDocView.GetDocViewContainerID() + "_selection_operation_bar";
                var copyBtnID = pDocView.GetDocViewContainerID() + "_selection_operation_copy";
                var divHtml = "<div class='fwr_text_selection_operation_bar fwr_hidden' id='" + selectionOperationDivID + "'>"

                                            + "<ul>"
                                                + "<li >"
                                                     + "<a id='" + copyBtnID + "' href='javascript:void(0);'>" + "Copy" + "</a>"
                                                + "</li>"
                                            + "</ul>"
                                        + "<div class='fwr_text_selection_operation_bar_r'/>"
                                   + "</div>";
                if (pFirstPageView != null && pFirstPageView.IsContentCreated()) {
                    $("#" + pFirstPageView.GetPageContainerID()).parent().parent().append(divHtml);
                }
                else {
                    jqPagesContainerDiv.append(divHtml);
                }
                _jqSelectionOperationBarDiv = $("#" + selectionOperationDivID);
                $("#" + copyBtnID).bind('click', function () {
                    if(FXREADER.CopyText(_self.GetSelectedText()))
                    {
                    	_self.HideSelectionOperationDiv();
                    }
                    //  window.prompt("Copy to clipboard: Ctrl+C, Enter", _self.GetSelectedText());
                });
            }
            return _jqSelectionOperationBarDiv;
        }
        
        this.GetTextPage = function(pageIndex)
        {
        	var pPage = _pTextPageList[pageIndex];
            if (!pPage) {
            	var pTextPage = _pReaderApp.GetTextManager().GetTextPage(pageIndex,true);
            	if(pTextPage != null)
            	{
            		pPage = new TEXT_PAGE();
            		pPage.pTextPage = pTextPage;
            		pPage.pageSelectInfo.SetTextPage(pTextPage);
            		_pTextPageList[pageIndex] = pPage;
            	}
            }
            return pPage;
        };
        
        /**
         * @ignore
        * @description 隐藏选完文本后操作的Div元素
        */
        this.HideSelectionOperationDiv = function () {
            var jqSelectionOperationBarDiv = _GetSelectionOperationDiv();
            jqSelectionOperationBarDiv.addClass("fwr_hidden");
        };
        /**
         * @ignore
        * @description 设置鼠标样式
        * @private
        * @param {FXREADER.TextSelect.CURSOR_STATE} cursor 设置鼠标样式值
        */
        function _SetCursor(cursor) {
            _cursorState = cursor;
            var pDocView = _pReaderApp.GetMainView().GetDocView();
            var jqDocViewContainer = $("#" + pDocView.GetDocViewContainerID());
            if (cursor == CURSOR_STATE.CURSOR_ARROW) {
                jqDocViewContainer.removeClass(_curCursorStyleName);
                jqDocViewContainer.css({ 'cursor': 'default' });
            }
            else if (cursor == CURSOR_STATE.CURSOR_SELECT) {
                jqDocViewContainer.removeClass(_curCursorStyleName);
                jqDocViewContainer.css({ 'cursor': 'text' });
            }
            else if (cursor == CURSOR_STATE.CURSOR_BOUND) {
                jqDocViewContainer.addClass('fwr_bound_cursor');
                _curCursorStyleName = "fwr_bound_cursor";
            }
            else if (cursor == CURSOR_STATE.CURSOR_QKSCH) {

            }
            else if (cursor == CURSOR_STATE.CURSOR_NORMAL) {
                jqDocViewContainer.removeClass(_curCursorStyleName);
                jqDocViewContainer.css({ 'cursor': 'text' });
            }
        }
   
    
        /**
        * @description 判断一个点是否在区域内
        * @private
        * @param {FXREADER.CPDF_Point} pdfpoint 一个点（PDF坐标系）
        * @param {FXREADER.CPDF_Rect对象数组} RectArray 区域数组
        * @return {Boolean} 在该区域内返回true,否则返回false;
        */
        function _IsPtInSelectedRect(pdfpoint, RectArray) {
            var PtInRect = false;
            for (var i = 0; i < RectArray.length; i++) {
                var Recti = RectArray[i];
                if (!FXREADER.RectUtils.IsEmpty(Recti)) {
                    if (FXREADER.RectUtils.PtInRect(Recti, pdfpoint.x, pdfpoint.y)) {
                        PtInRect = true;
                        break;
                    }
                }
            }
            return PtInRect;
        }
        /**
        * @description 计算选择区域
        * @private
        * @param {FXREADER.PDFData.Text.CARET_INFO} caret1 开始时插入符信息
        * @param {FXREADER.PDFData.Text.CARET_INFO} caret2 结束时插入符信息
        * @param {Number} iPage1 开始时所在页面索引（从0开始）
        * @param {Number} iPage2 结束时所在页面索引（从0开始）
        * @return {Boolean} 成功返回true,否则返回false;
        */
        function _CalcSelect(caret1, caret2, iPage1, iPage2) {
            if (iPage1 < 0 || iPage2 < 0 || caret1.index == -1 || caret2.index == -1)
                return false;
            var select_array = [];
            select_array = CommonTools.Clone(_pSLArray);
            _pSLArray.length = 0;
            var count = select_array.length;
            for (var i = 0; i < count; i++) {
                if (iPage1 > iPage2) {
                    if (select_array[i] > iPage1 || select_array[i] < iPage2) {
                        var pPage = _self.GetTextPage(select_array[i]);
                        if (!pPage)
                            return false;
                        pPage.pageSelectInfo.ClearSelectRect();
                        _pSLArray.push(select_array[i]);
                    }
                }
                else {
                    if (select_array[i] > iPage2 || select_array[i] < iPage1) {
                        var pPage = _self.GetTextPage(select_array[i]);
                        if (!pPage)
                            return false;
                        pPage.pageSelectInfo.ClearSelectRect();
                        _pSLArray.push(select_array[i]);
                    }
                }
            }

            if (iPage1 < iPage2) {
                var pPage1 = _self.GetTextPage(iPage1);
                var pPage2 = _self.GetTextPage(iPage2);
                if (!pPage1 || !pPage2)
                    return false;
                pPage1.pageSelectInfo.AddSLRectByIndex(caret1.bLeft == false ? caret1.index + 1 : caret1.index, pPage1.pTextPage.GetSize() - 1);
                _pSLArray.push(iPage1);

                for (var i = iPage1 + 1; i < iPage2; i++) {
                    var pPage = _self.GetTextPage(i);
                    if (!pPage)
                        return false;
                    pPage.pageSelectInfo.SelectAll();
                    if (pPage.pageSelectInfo.IsSelected()) {
                        _pSLArray.push(i);
                    }
                }
                pPage2.pageSelectInfo.AddSLRectByIndex(0, caret2.bLeft ? caret2.index - 1 : caret2.index);
                _pSLArray.push(iPage2);

                if (_pSLArray.length == 0) {
                    return false;
                }
                return true;
            }
            if (iPage1 > iPage2) {
            	 var pPage1 = _self.GetTextPage(iPage1);
                 var pPage2 = _self.GetTextPage(iPage2);
                if (!pPage1 || !pPage2)
                    return false;
                pPage2.pageSelectInfo.AddSLRectByIndex(caret2.bLeft == false ? caret2.index + 1 : caret2.index, pPage2.pTextPage.GetSize() - 1);
                _pSLArray.push(iPage2);

                for (var i = iPage2 + 1; i < iPage1; i++) {
                    var pPage = _self.GetTextPage(i);
                    if (!pPage)
                        return false;
                    pPage.pageSelectInfo.SelectAll();
                    if (pPage.pageSelectInfo.IsSelected()) {
                        _pSLArray.push(i);
                    }
                }

                pPage1.pageSelectInfo.AddSLRectByIndex(0, caret1.bLeft ? caret1.index - 1 : caret1.index);
                _pSLArray.push(iPage1);

                if (_pSLArray.length == 0) {
                    return false;
                }
                return true;
            }

            var index1 = caret1.index;
            var index2 = caret2.index;
            if (caret1.index < caret2.index) {
                if (caret1.bLeft == false) {
                    index1++;
                }
                if (caret2.bLeft == true) {
                    index2--;
                }
                var pPage = _self.GetTextPage(iPage1);
                if (!pPage)
                    return false;
                pPage.pageSelectInfo.AddSLRectByIndex(index1, index2);
                _pSLArray.push(iPage1);
                return true;
            }
            if (caret1.index > caret2.index) {
                if (caret2.bLeft == false) {
                    index2++;
                }
                if (caret1.bLeft == true) {
                    index1--;
                }
                var pPage = _self.GetTextPage(iPage1);
                if (!pPage)
                    return false;
                pPage.pageSelectInfo.AddSLRectByIndex(index2, index1);
                _pSLArray.push(iPage1);
                return true;
            }
            if (caret1.index == caret2.index && caret1.bLeft != caret2.bLeft) {
                var pPage = _self.GetTextPage(iPage1);
                if (!pPage)
                    return false;
                pPage.pageSelectInfo.AddSLRectByIndex(caret1.index, caret2.index);
                _pSLArray.push(iPage1);
                return true;
            }
            if (caret1.index == caret2.index && caret1.bLeft == caret2.bLeft) {
                if (select_array.length == 0) {
                    return false;
                }
                var pPage = _self.GetTextPage(iPage1);
                if (!pPage)
                    return false;
                pPage.pageSelectInfo.ClearSelectRect();
                _pSLArray.push(iPage1);
                return true;
            }
            return false;
        }
        /**
        * @description 计算范围选择标记的区域
        * @private
        * @param {FXREADER.CPDF_Point} pt1 开始点
        * @param {FXREADER.CPDF_Point} pt2 结束点
        * @param {Number} iPage1 开始时所在页面索引（从0开始）
        * @param {Number} iPage2 结束时所在页面索引（从0开始）
        * @return {Boolean} 成功返回true,否则返回false;
        */
        function _CalcSLBound(pt1, pt2, iPage1, iPage2) {
            if (pt1.Equal(pt2) && iPage1 == iPage2) {
                if (_pSLArray.length == 0) {
                    return false;
                }
                else {
                    // m_pReaderDocument->ClearSelection();
                    _self.ClearSelection();
                    return false;
                }
            }
            var pdfpoint1 = new CPDF_Point(0, 0);
            var pdfpoint2 = new CPDF_Point(0, 0);
            pdfpoint1 = _pCurPageView.DocPt2PDF(pt1);
            pdfpoint2 = _pCurPageView.DocPt2PDF(pt2);
            var rect = new CPDF_Rect(pdfpoint1.x, pdfpoint1.y, pdfpoint2.x, pdfpoint2.y);
            var pPage = _self.GetTextPage(iPage1);
            if (!pPage)
                return false;
            //	CPDF_Rect rect1(0,0,1000,1000);
            pPage.pageSelectInfo.AddSLRect(rect);
            _pSLArray.length = 0;
            _pSLArray.push(iPage1);
            return true;
        }
        /**
        * @description left mouse button down event
        * @param {object} event event
        */
        this.Select_OnLButtonDown = function (event) {
            _beginSelectionPoint.x = event.pageX;
            _beginSelectionPoint.y = event.pageY;
            var pPageView = null, ptOffsetCurPageView = new CPDF_Point(0, 0);
            pPageView = FXREADER.TextSelect.CReader_TextSelectTool.GetPageViewInfoByMouseEvent(_pReaderApp, event, ptOffsetCurPageView, true, false);
            if (pPageView == null) {
                var pDocView = _pReaderApp.GetMainView().GetDocView();
                var tmpPt = new CPDF_Point(event.pageX, event.pageY);
                var docViewRect = pDocView.GetDocViewClientRect();
                if (FXREADER.RectUtils.PtInRect(docViewRect, tmpPt)) {
                    this.ClearSelection();
                }
                return;
            }
            FXREADER.Common.PreventDefaults(event, true);
            _bLButtomDown = true;
            var pdfpoint = new CPDF_Point(0, 0);
            pdfpoint = pPageView.DocPt2PDF(ptOffsetCurPageView);
            var rect_array = [];
            var iPage = pPageView.GetPageIndex();
            if (iPage < 0) {
                return;
            }

            var pPage = _self.GetTextPage(iPage);
            if (!pPage)
                return;
            pPage.pageSelectInfo.GetSelectPDFRect(rect_array);
            var PtInRect = _IsPtInSelectedRect(pdfpoint, rect_array); //如果点在已经被选择的文本区域里;
            if (PtInRect) {
                _select_mode = SELECT_STATE.SELECT_MODE_IN_SELSETED;
                _SetCursor(CURSOR_STATE.CURSOR_ARROW);
                return;
            }

            var char_index = pPage.pTextPage.GetIndexByPoint(pdfpoint);
            if (char_index != -1) {
                var caret_info = new CARET_INFO();
                pPage.pTextPage.GetCaretAtPoint(pdfpoint, caret_info);
                if (caret_info.index == -1) {
                    //旋转字体有可能返回-1值;
                    char_index = -1;
                }
            }

            //[得到ALT键的状态; 高位:1是down,0是up ;决定了是否必须采用BOUND方式]
            var nAltKey = event.altKey;
            //[鼠标在任何一个TextObject处按下]//[只要SLTextObject存在就认为是NORMAL方式]
            if (_bSLBound) {
                //清空文本选择;
                //m_pReaderDocument->ClearSelection();
                this.ClearSelection();
                _bSLBound = false;
                _CurCaretInfo.index = -1;
            }
            if (char_index >= 0 && !nAltKey)//Alt up //在文本上且没有按住Alt键（按住Alt键意思：用选框选择）
            {
                var bShift = event.shiftKey;
                _select_mode = SELECT_STATE.SELECT_MODE_SELECTING;
                _SetCursor(CURSOR_STATE.CURSOR_SELECT);
                if (_StartCaretInfo.index < 0) {
                    if (_pCurPageView != pPageView) {
                        _pCurPageView = pPageView;
                    }
                    pPage.pTextPage.GetCaretAtPoint(pdfpoint, _CurCaretInfo);
                    _StartCaretInfo = CommonTools.Clone(_CurCaretInfo);
                    _nStartPageIndex = _pCurPageView.GetPageIndex();
                    if (_CurCaretInfo.index < 0)
                        return;
                }
                else if (!bShift) //当没有按住Shift键
                {
                    if (_StartCaretInfo.index < 0)
                        return;
                    //  m_pReaderDocument->ClearSelection();
                    this.ClearSelection();
                    var caret_info = new CARET_INFO();
                    pPage.pTextPage.GetCaretAtPoint(pdfpoint, caret_info);

                    if (_pCurPageView != pPageView) {
                        _pCurPageView = pPageView;
                    }
                    _CurCaretInfo = caret_info;
                    _StartCaretInfo = CommonTools.Clone(_CurCaretInfo);
                    _nStartPageIndex = _pCurPageView.GetPageIndex();
                }
                else if (bShift) //只要按住 按住Shift键;
                {
                    if (_StartCaretInfo.index == -1)
                        return;
                    var caret_info = new CARET_INFO();
                    pPage.pTextPage.GetCaretAtPoint(pdfpoint, caret_info);
                    if (_pCurPageView != pPageView) {
                        _pCurPageView = pPageView;
                    }
                    _CurCaretInfo = caret_info;
                    var bRet = _CalcSelect(_StartCaretInfo, _CurCaretInfo, _nStartPageIndex, _pCurPageView.GetPageIndex());
                    if (bRet) {
                        //DrawSelection(pDC);
                    }
                }

            }
            else //在文本上且按alt键或者不在文本上 （选框选择）;
            {
                //清空文本选择;
                this.ClearSelection();
                _pCurPageView = pPageView;

                if (char_index >= 0) {
                    pPage.pTextPage.GetCaretAtPoint(pdfpoint, _StartCaretInfo);
                    _nStartPageIndex = _pCurPageView.GetPageIndex();
                }
                _CurCaretInfo.index = -1;
                _startwindowpoint = _endwindowpoint = ptOffsetCurPageView;
                _select_mode = SELECT_STATE.SELECT_MODE_BOUND;
                _SetCursor(CURSOR_STATE.CURSOR_BOUND);
            }
        };
        /**
        * @description 处理选择文本鼠标左键弹起事件
        * @param {object} event 鼠标事件对象
        * @param {function(object)} callbackSelected 选择文本后的回调函数
        */
        this.Select_OnLButtonUp = function (event, callbackSelected) {
            /************************************************************************/
            /* Text模块特殊处理。一定要在有文本选择的情况下才会触发SetCurSelection;*/
            /************************************************************************/
            /*  if(this.IsSelected() && m_pReaderDocument->GetCurSelection() != this)
            m_pReaderDocument->SetCurSelection("Text",this);*/
            _bLButtomDown = false;
            _HideBoundBox();
            var pPageView = null, ptOffsetCurPageView = new CPDF_Point(0, 0);
            pPageView = FXREADER.TextSelect.CReader_TextSelectTool.GetPageViewInfoByMouseEvent(_pReaderApp, event, ptOffsetCurPageView, true, false);
            if (pPageView == null)
                return;
            if (_select_mode == SELECT_STATE.SELECT_MODE_IN_SELSETED) {
                var rect_array = [];
                var iPage = pPageView.GetPageIndex();
                if (iPage < 0)
                    return;
                var pPage = _self.GetTextPage(iPage);
                if (!pPage)
                    return;
                pPage.pageSelectInfo.GetSelectPDFRect(rect_array);
                var pdfpoint = new CPDF_Point(0, 0);
                pdfpoint = pPageView.DocPt2PDF(ptOffsetCurPageView);
                var PtInRect = _IsPtInSelectedRect(pdfpoint, rect_array); //如果点在已经被选择的文本区域里;
                if (PtInRect) {
                    this.ClearSelection();
                }
                else if (_bSearch) {
                    _bSearch = false;
                    /* IReader_QucikSearchMgr* pMgr = m_pReaderDocument->GetApp()->GetQucikSearchMgr();
                    if(pMgr) 
                    {
                    if(pMgr->OnCanDoSearch())
                    pMgr->OnStartSearch(GetSelectedText());
                    }*/
                }
            }
            var pdfpoint = new CPDF_Point(0, 0);
            pdfpoint = pPageView.DocPt2PDF(ptOffsetCurPageView);
            var iPage = pPageView.GetPageIndex();
            if (iPage < 0) return;
            var pPage = _self.GetTextPage(iPage);
            if (!pPage) return;
            var rect_array = [];
            pPage.pageSelectInfo.GetSelectPDFRect(rect_array);
            var PtInRect = _IsPtInSelectedRect(pdfpoint, rect_array); //如果点在已经被选择的文本区域里;
            if (PtInRect && _cursorState != CURSOR_STATE.CURSOR_ARROW) {
                _SetCursor(CURSOR_STATE.CURSOR_ARROW);
            }
            else if (!PtInRect && _cursorState != CURSOR_STATE.CURSOR_SELECT) {
                _SetCursor(CURSOR_STATE.CURSOR_SELECT);
            }
            if (_select_mode == SELECT_STATE.SELECT_MODE_BOUND) {
                //DrawBoundBox(_startwindowpoint,_endwindowpoint,RGB (255,255,255));
            }
            // DrawOtherDocuments(pPageView);
            _select_mode = SELECT_STATE.SELECT_MODE_NORMAL;
           
            if (this.IsSelected()) {
                if (callbackSelected) {
                    callbackSelected(event);
                } else {
                	this.HighLightRects();
                    if (event.pageY >= _beginSelectionPoint.y) {
                        if (rect_array.length > 0)
                            _ShowOperationBar(pPageView, rect_array, true);
                        else {
                            var pTmpPageView = _GetPrevSelectPageRects(rect_array, iPage);
                            if (pTmpPageView)
                                _ShowOperationBar(pTmpPageView, rect_array, true);
                        }

                    }
                    else {
                        if (rect_array.length > 0)
                            _ShowOperationBar(pPageView, rect_array, false);
                        else {
                            var pTmpPageView = _GetNextSelectPageRects(rect_array, iPage);
                            if (pTmpPageView)
                                _ShowOperationBar(pTmpPageView, rect_array, false);
                        }
                    }
                }
            }
            _bLButtomDown = false;
        };
        /**
        * @description 获取当前页上一页选择文本的区域
        * @private
        * @param {Array.<{FXREADER.CPDF_Rect}>} rect_array 选择文本区域数组
        * @param {Number} iPageIndex 当前页面索引（从0开始）
        * @return {FXREADER.PDFView.CPDF_PageView} 返回上一页的页面视图对象;
        */
        function _GetPrevSelectPageRects(rect_array, iPageIndex) {
            var count = _pSLArray.length;
            var pPageView = null;
            for (var i = count - 1; i >= 0; i--) {
                var index = _pSLArray[i];
                if (index >= iPageIndex)
                    continue;
                var pPage = _self.GetTextPage(index);
                if (!pPage)
                    continue;
                if (pPage.pageSelectInfo.GetSelectPDFRect(rect_array) != 0) {
                    pPageView = _pReaderApp.GetMainView().GetDocView().GetPageView(index);
                    return pPageView;
                }
            }
            return pPageView;
        }
        /**
        * @description 获取当前页下一页选择文本的区域
        * @private
        * @param {Array.<{FXREADER.CPDF_Rect}>} rect_array 选择文本区域数组
        * @param {Number} iPageIndex 当前页面索引（从0开始）
        * @return {FXREADER.PDFView.CPDF_PageView} 返回下一页的页面视图对象;
        */
        function _GetNextSelectPageRects(rect_array, iPageIndex) {
            var count = _pSLArray.length;
            var pPageView = null;
            for (var i = 0; i < count; i++) {
                var index = _pSLArray[i];
                if (index <= iPageIndex)
                    continue;
                var pPage = _self.GetTextPage(index);
                if (!pPage)
                    continue;
                if (pPage.pageSelectInfo.GetSelectPDFRect(rect_array) != 0) {
                    pPageView = _pReaderApp.GetMainView().GetDocView().GetPageView(index);
                    return pPageView;
                }
            }
            return pPageView;
        }
        /**
        * @description 获取页面相应标记文本选中区域Div的ID
        * @private
        * @param {FXREADER.PDFView.CPDF_PageView} pPageView 页面视图对象
        * @return {String} 返回标记文本选中区域Div的ID;
        */
        function _GetHighlightDivID(pPageView) {
            return pPageView.GetPageContainerID() + "_TextHighLights";
        }
        /**
        * @description 显示文本选择后的操作Div
        * @private
        * @param {object} event 鼠标事件对象
        */
        function _ShowOperationBarV2(event) {
            var jqSelectionOperationDiv = _GetSelectionOperationDiv();
            if (!jqSelectionOperationDiv.hasClass("fwr_hidden"))
                return;
            jqSelectionOperationDiv.removeClass("fwr_hidden");
            jqSelectionOperationDiv.css({ left: event.pageX, top: event.pageY });
        }
        /**
        * @description 显示文本选择后的操作Div
        * @private
        * @param {FXREADER.PDFView.CPDF_PageView} pPageView 页面视图对象
        * @param {Array.<{FXREADER.CPDF_Rect}>} select_pdf_rects 页面选择区域数组
        * @param {Boolean} bSelectDown 是否是向下选择
        */
        function _ShowOperationBar(pPageView, select_pdf_rects, bSelectDown) {
            var jqSelectionOperationDiv = _GetSelectionOperationDiv();
            if (!jqSelectionOperationDiv.hasClass("fwr_hidden"))
                return;
            var curNearRect = null;
            if (bSelectDown) {
                curNearRect = _GetLowwerRightCornerRect(select_pdf_rects);
            }
            else {
                curNearRect = _GetUpperLeftCornerRect(select_pdf_rects);
            }
            if (curNearRect == null)
                return;
            curNearRect = pPageView.PDF2Doc(curNearRect, true);
            var pageOffsetHtmlDoc = $("#" + pPageView.GetPageContainerID()).offset();
            var leftPos = 0, topPos = 0;
            if (bSelectDown) {
                leftPos = curNearRect.right + pageOffsetHtmlDoc.left;
                topPos = curNearRect.bottom + pageOffsetHtmlDoc.top;
            }
            else {
                leftPos = curNearRect.left + pageOffsetHtmlDoc.left;
                topPos = curNearRect.top + pageOffsetHtmlDoc.top;
            }

            jqSelectionOperationDiv.removeClass("fwr_hidden");
            var pDocView = _pReaderApp.GetMainView().GetDocView();
            var docViewRect = pDocView.GetDocViewClientRect();
            var ScrollApi = pDocView.GetScrollApi();
            var scrollPosX = 0, scrollPosY = 0;
            if (ScrollApi) {
                scrollPosX = ScrollApi.getContentPositionX();
                scrollPosY = ScrollApi.getContentPositionY();
            }
            leftPos = leftPos - docViewRect.left + scrollPosX;
            topPos = topPos - docViewRect.bottom + scrollPosY;
            if (!bSelectDown) {
                var divWidth = jqSelectionOperationDiv.width();
                var divHeight = jqSelectionOperationDiv.height();
                leftPos = leftPos - divWidth;
                topPos = topPos - divHeight;
            }
            jqSelectionOperationDiv.css({ left: leftPos, top: topPos });
        }
        /**
        * @description 获取所有选择文本字符串
        * @return {String} 返回选择文本字符串;
        */
        this.GetSelectedText = function () {
            var AllSelectText = "";
            var nCount = _pSLArray.length;
            for (var i = 0; i < nCount; i++) {
                var index = _pSLArray[i];
                var pSLPage = _self.GetTextPage(index);
                if (pSLPage && pSLPage.pTextPage) {
                    AllSelectText += pSLPage.pageSelectInfo.GetSelectedText();
                    if (i != nCount - 1) {
                        AllSelectText += FXREADER.TEXT_RETURN_LINEFEED;
                    }
                }
            }
            return AllSelectText;
        };
        /**
        * @description 获取选择文本区域信息
        * @return {Array.<{FXREADER.TextSelect.TEXT_SELECTED_INFO}>} 选择文本区域信息;
        */
        this.GetSelectedTextRectInfo = function () {
            var selected_pagerect_array = [];
            var nCount = _pSLArray.length;
            for (var i = 0; i < nCount; i++) {
                var index = _pSLArray[i];
                var pSLPage = _self.GetTextPage(index);
                if (!pSLPage) {
                    continue;
                }
                if (pSLPage.pageSelectInfo.IsSelected()) {
                    var rect_array = [];
                    pSLPage.pageSelectInfo.GetSelectPDFRect(rect_array);
                    var tmpPagerect = new FXREADER.TextSelect.TEXT_SELECTED_INFO();
                    tmpPagerect.pageIndex = index;
                    tmpPagerect.selectedRectArray = rect_array;
                    selected_pagerect_array.push(tmpPagerect);
                }
            }
            return selected_pagerect_array;
        };
        /**
        * @description 清除所有选择文本，并隐藏选择文本后的操作Div
        */
        this.ClearSelection = function () {
            var nCount = _pSLArray.length;
            for (var i = 0; i < nCount; i++) {
                var index = _pSLArray[i];
                var pSLPage = _self.GetTextPage(index);
                if (pSLPage) {
                    pSLPage.pageSelectInfo.ClearSelectRect();
                    this.ClearHighLightText(_pReaderApp.GetMainView().GetDocView().GetPageView(index));
                }
            }
            this.HideSelectionOperationDiv();
        };
        /**
        * @description 清除某页面视图的选择文本
        * @param {FXREADER.PDFView.CPDF_PageView} pPageView 页面视图对象
        */
        this.ClearHighLightText = function (pPageView) {
            if (pPageView != null && pPageView.IsContentCreated()) {
                var highLightDivID = _GetHighlightDivID(pPageView);
                var jqHighlightDiv = $("#" + highLightDivID);
                if (jqHighlightDiv != null) {
                    jqHighlightDiv.empty();
                }
            }
        };
        /**
        * @description 获取某区域数组内，左上角区域（最上）
        * @private
        * @param {Array.<{FXREADER.CPDF_Rect}>} pdf_rects 区域数组
        * @return {FXREADER.CPDF_Rect} 返回左上角区域;
        */
        function _GetUpperLeftCornerRect(pdf_rects) {
            var destRect = null;
            if (pdf_rects == null || pdf_rects.length <= 0)
                return null;
            destRect = pdf_rects[0];
            FXREADER.RectUtils.Normalize(destRect);
            for (var i = 1; i < pdf_rects.length; i++) {
                var rect = pdf_rects[i];
                FXREADER.RectUtils.Normalize(rect);
                if (rect.top > destRect.top) { //pdf坐标系从下到上，所以取最大的top，转到document里，就是最小的top
                    destRect = rect;
                    continue;
                }
                else if (rect.top == destRect.top) {
                    if (rect.left < destRect.left) {
                        destRect = rect;
                        continue;
                    }
                }
            }
            return destRect;
        }
        /**
        * @description 获取某区域数组内，右下角区域（最下）
        * @private
        * @param {Array.<{FXREADER.CPDF_Rect}>} pdf_rects 区域数组
        * @return {FXREADER.CPDF_Rect} 右下角区域;
        */
        function _GetLowwerRightCornerRect(pdf_rects) {
            var destRect = null;
            if (pdf_rects == null || pdf_rects.length <= 0)
                return null;
            destRect = pdf_rects[0];
            FXREADER.RectUtils.Normalize(destRect);
            for (var i = 1; i < pdf_rects.length; i++) {
                var rect = pdf_rects[i];
                FXREADER.RectUtils.Normalize(rect);
                if (rect.bottom < destRect.bottom) { //pdf坐标系从下到上，所以取最小的bottom，转到document里，就是最大的bottom
                    destRect = rect;
                    continue;
                }
                else if (rect.bottom == destRect.bottom) {
                    if (rect.right > destRect.right) {
                        destRect = rect;
                        continue;
                    }
                }
            }
            return destRect;
        }
        /**
        * @description 添加选择文本区域到相应页面标记Div内
        * @private
        * @param {FXREADER.PDFView.CPDF_PageView} pPageView 页面视图对象
        * @param {Array.<{FXREADER.CPDF_Rect}>} highlightRects 区域数组
        */
        function _HighLightRects(pPageView, highlightRects) {
            if (pPageView == null || !pPageView.IsContentCreated() || highlightRects == null)
                return false;
            var highLightDivID = _GetHighlightDivID(pPageView);
            if (_PageHighlightCreatedList[pPageView.GetPageIndex()] == null) {
                var highLightDivHtml = "<div id='" + highLightDivID + "' style='z-index:7;'></div>";
                var jqPageView = $("#" + pPageView.GetPageContainerID());
                jqPageView.append(highLightDivHtml);
                _PageHighlightCreatedList[pPageView.GetPageIndex()] = true;
            }
            var jqHighlightDiv = $("#" + highLightDivID);
            jqHighlightDiv.empty(); //清空原有的div内容;

            var highLightDivContent = "";
            for (var i = 0; i < highlightRects.length; i++) {
                var rect = highlightRects[i];
                FXREADER.RectUtils.Normalize(rect);
                rect = pPageView.PDF2Doc(rect, true);
                highLightDivContent += "<div id='" + "text_highlight_" + pPageView.GetPageIndex() + "_" + i + "'class='fwr_text_highlight' style='left:" +
                                 rect.left + "px;top:" + rect.top + "px;width:" + FXREADER.RectUtils.Width(rect) + "px;height:" + FXREADER.RectUtils.Height(rect) + "px' ></div>";
            }
            jqHighlightDiv.append(highLightDivContent);
        }
        /**
        * @description 隐藏所有选择文本区域标记Div
        */
        this.HideHighLightRects = function () {
            var nCount = _pSLArray.length;
            for (var i = 0; i < nCount; i++) {
                var index = _pSLArray[i];
                var pSLPage = _self.GetTextPage(index);
                if (pSLPage) {
                    this.ClearHighLightText(_pReaderApp.GetMainView().GetDocView().GetPageView(index));
                }
            }
        };
        /**
        * @description 显示所有选择文本区域标记Div
        */
        this.HighLightRects = function () {
            var nCount = _pSLArray.length;
            for (var i = 0; i < nCount; i++) {
                var index = _pSLArray[i];
                var pSLPage = _self.GetTextPage(index);
                if (!pSLPage) {
                    continue;
                }
                var rect_array = [];
                pSLPage.pageSelectInfo.GetSelectPDFRect(rect_array);
                _HighLightRects(_pReaderApp.GetMainView().GetDocView().GetPageView(index), rect_array);
            }
        };
        /**
        * @description 处理选择文本鼠标左键弹起双击事件
        * @param {object} event 鼠标事件对象
        * @param {function(object)} callbackSelected 选择文本后的回调函数
        */
        this.Select_OnLButtonDblClk = function (event, callbackSelected) {

            if (_select_mode == SELECT_STATE.SELECT_MODE_BOUND) {
                return;
            }
            var pPageView = null, ptOffsetCurPageView = new CPDF_Point(0, 0);
            pPageView = FXREADER.TextSelect.CReader_TextSelectTool.GetPageViewInfoByMouseEvent(_pReaderApp, event, ptOffsetCurPageView, true, false);
            if (pPageView == null)
                return;
            _pCurPageView = pPageView;
            var iPage = -1;
            iPage = pPageView.GetPageIndex();
            if (iPage < 0) return;
            var pPage = _self.GetTextPage(iPage);
            if (!pPage)
                return;
            var pdfpoint = new CPDF_Point(0, 0);
            pdfpoint = pPageView.DocPt2PDF(ptOffsetCurPageView);
            var char_index = pPage.pTextPage.GetIndexByPoint(pdfpoint);
            if (char_index != -1) {
                var caret_info = new CARET_INFO();
                pPage.pTextPage.GetCaretAtPoint(pdfpoint, caret_info);
                if (caret_info.index == -1) {
                    //旋转字体有可能返回-1值;
                    char_index = -1;
                }
            }
            if (char_index != -1) {
                var nStart = -1;
                var nEnd = -1;
                //下面先注释，暂不处理
                var wordRangeInfo = new BOUND_INFO(-1, -1);
                if (pPage.pTextPage.GetWordRange(char_index, wordRangeInfo)) {
                    if (wordRangeInfo.start_index == -1 || wordRangeInfo.end_index == -1)
                        return;
                    pPage.pageSelectInfo.AddSLRectByIndex(wordRangeInfo.start_index, wordRangeInfo.end_index);
                    _pSLArray.length = 0;
                    _pSLArray.push(iPage);

                    if (callbackSelected) {
                        callbackSelected(event);
                    } else {
                        var rect_array = [];
                        pPage.pageSelectInfo.GetSelectPDFRect(rect_array);
                        _ShowOperationBar(pPageView, rect_array, true);

                        this.HighLightRects();
                    }
                }
            }
        };
        /**
        * @description 处理选择文本鼠标移动事件
        * @param {object} event 鼠标事件对象
        */
        this.Select_OnMouseMove = function (event) {
            var pPageView = null, ptOffsetCurPageView = new CPDF_Point(0, 0);
            pPageView = FXREADER.TextSelect.CReader_TextSelectTool.GetPageViewInfoByMouseEvent(_pReaderApp, event, ptOffsetCurPageView, true, true);
            if (pPageView == null)
                return;

            var pdfpoint = new CPDF_Point(0, 0);
            pdfpoint = pPageView.DocPt2PDF(ptOffsetCurPageView);
            var iPage = -1;
            iPage = pPageView.GetPageIndex();
            if (iPage < 0) return;

            var pPage = _self.GetTextPage(iPage);
            if (!pPage) {
                if (_bLButtomDown)
                    FXREADER.Common.PreventDefaults(event, true);
                return;
            }

            //    if (!_bLButtomDown) //测试
            //        return;
            if (_bLButtomDown)
                FXREADER.Common.PreventDefaults(event, true);
            var rect_array = [];
            pPage.pageSelectInfo.GetSelectPDFRect(rect_array);
            var PtInRect = _IsPtInSelectedRect(pdfpoint, rect_array); //如果点在已经被选择的文本区域里;
            var char_index = pPage.pTextPage.GetIndexByPoint(pdfpoint);
            if (char_index != -1) {
                var caret_info = new CARET_INFO();
                pPage.pTextPage.GetCaretAtPoint(pdfpoint, caret_info);
                if (caret_info.index == -1) {
                    //旋转字体有可能返回-1值;
                    char_index = -1;
                }
            }
            var nAltKey = event.altKey;
            //[得到ALT键的状态; 高位:1是down,0是up ;决定了是否必须采用BOUND方式]
            if (_select_mode == SELECT_STATE.SELECT_MODE_NORMAL) {
                if (PtInRect) {
                    _SetCursor(CURSOR_STATE.CURSOR_ARROW);
                }
                else if (char_index >= 0 && !nAltKey) {
                    _SetCursor(CURSOR_STATE.CURSOR_SELECT);

                }
                else if (char_index >= 0 && nAltKey) {
                    _SetCursor(CURSOR_STATE.CURSOR_BOUND);
                }
                else {
                    _SetCursor(CURSOR_STATE.CURSOR_NORMAL);
                }
                return;
            }
            if (_select_mode == SELECT_STATE.SELECT_MODE_IN_SELSETED) {
                /*  IReader_QucikSearchMgr* pMgr = m_pReaderDocument->GetApp()->GetQucikSearchMgr();
                if(!PtInRect && pMgr && pMgr->OnCanDoSearch() )
                {
                _bSearch = TRUE;
                _SetCursor(CURSOR_QKSCH);
                }
                else
                {
                _SetCursor(CURSOR_ARROW);
                }*/
                return;
            }

            if (_select_mode == SELECT_STATE.SELECT_MODE_BOUND) {
                if (_cursorState != CURSOR_STATE.CURSOR_BOUND)
                    return;
                _bSLBound = true;
                //  DrawBoundBox(_startwindowpoint,_endwindowpoint,RGB (255,255,255));
                // var rect = new CPDF_Rect(_startwindowpoint, _endwindowpoint);
                _endwindowpoint = ptOffsetCurPageView;
                if (iPage != _pCurPageView.GetPageIndex()) {
                    if (iPage < _pCurPageView.GetPageIndex()) {
                        _endwindowpoint.y = 0;
                    }
                    else {
                        _endwindowpoint.y = _pCurPageView.GetPageViewHeight();
                    }

                }

                if (_CalcSLBound(_startwindowpoint, _endwindowpoint, _pCurPageView.GetPageIndex(), iPage)) {
                    //    DrawSelection();
                    this.HighLightRects();
                    pPage.pageSelectInfo.GetFirstCaretInfo(_CurCaretInfo);
                }
                _endSelectionPoint.x = event.pageX;
                _endSelectionPoint.y = event.pageY;
                _DrawBoundBox();
                //    DrawBoundBox(_startwindowpoint,point,RGB(0,0,0));
            }
            else if (_select_mode == SELECT_STATE.SELECT_MODE_SELECTING) {
                if (_cursorState != CURSOR_STATE.CURSOR_SELECT)
                    return;
                if (!_pCurPageView || _CurCaretInfo.index < 0)
                    return;

                var caret_info = new CARET_INFO();
                var bRet = pPage.pTextPage.GetCaretAtPoint(pdfpoint, caret_info);
                if (_pCurPageView != pPageView) {
                    _pCurPageView = pPageView;
                }
                if (bRet) {
                    _CurCaretInfo = caret_info;
                    if (_CalcSelect(_StartCaretInfo, _CurCaretInfo, _nStartPageIndex, _pCurPageView.GetPageIndex())) {
                        //      DrawSelection();
                        this.HighLightRects();
                    }
                }

                // this.HighLightRects(pPageView, rect_array);
                var curPt = new CPDF_Point(event.pageX, event.pageY);
                _ScrollPage(curPt);
            }
        };
        /**
        * @description 显示范围选择区域标记Div
        * @private
        */
        function _DrawBoundBox() {
            var jqBoundSelection = _GetBoundSelectionRectDiv();
            var x = Math.abs(_endSelectionPoint.x - _beginSelectionPoint.x);
            var y = Math.abs(_endSelectionPoint.y - _beginSelectionPoint.y);
            var l = null, t = null;
            // A part
            if (_endSelectionPoint.x < _beginSelectionPoint.x && _endSelectionPoint.y < _beginSelectionPoint.y) {
                l = _endSelectionPoint.x;
                t = _endSelectionPoint.y;
            }

            // B part
            if (_endSelectionPoint.x > _beginSelectionPoint.x && _endSelectionPoint.y < _beginSelectionPoint.y) {
                l = _beginSelectionPoint.x;
                t = _endSelectionPoint.y;
            }

            // C part
            if (_endSelectionPoint.x < _beginSelectionPoint.x && _endSelectionPoint.y > _beginSelectionPoint.y) {
                l = _endSelectionPoint.x;
                t = _beginSelectionPoint.y;
            }

            // D part
            if (_endSelectionPoint.x > _beginSelectionPoint.x && _endSelectionPoint.y > _beginSelectionPoint.y) {
                l = _beginSelectionPoint.x;
                t = _beginSelectionPoint.y;
            }

            var pDocView = _pReaderApp.GetMainView().GetDocView();
            var docViewRect = pDocView.GetDocViewClientRect();
            var ScrollApi = pDocView.GetScrollApi();
            var scrollPosX = 0, scrollPosY = 0;
            if (ScrollApi) {
                scrollPosX = ScrollApi.getContentPositionX();
                scrollPosY = ScrollApi.getContentPositionY();
            }
            l = l - docViewRect.left + scrollPosX;
            t = t - docViewRect.bottom + scrollPosY;
            jqBoundSelection.css({ left: l, top: t, width: x, height: y });
            jqBoundSelection.removeClass("fwr_hidden");
        }
        /**
        * @description 隐藏范围选择区域标记Div
        * @private
        */
        function _HideBoundBox() {
            var jqBoundSelection = _GetBoundSelectionRectDiv();
            jqBoundSelection.addClass("fwr_hidden");
        }
        /**
        * @description 依据一个点，将滚动条滚动到该点所在页面显示
        * @private
        * @param {FXREADER.CPDF_Point} point 一个点对象
        * @return {Boolean} 成功返回true,否则返回false;
        */
        function _ScrollPage(point) {
            if (!_pCurPageView) {
                console.log('page null');
                return false;
            }
            var pDocView = _pReaderApp.GetMainView().GetDocView();
            var jqDocView = $("#" + pDocView.GetDocViewContainerID());
            //var docViewScrollTop = jqDocView.scrollTop();
            var docHeight = jqDocView.height(), docWidth = jqDocView.width() - 16,
                    docOffset = jqDocView.offset();
            var ScrollApi = pDocView.GetScrollApi();
            if (ScrollApi) {
                if (ScrollApi.getIsScrollableV())
                    docWidth -= 16;
                if (ScrollApi.getIsScrollableH())
                    docHeight -= 16;
            }


            var docViewRect = new CPDF_Rect(docOffset.left, docOffset.top, docOffset.left + docWidth, docOffset.top + docHeight);
            FXREADER.RectUtils.Normalize(docViewRect);
            if (FXREADER.RectUtils.PtInRect(docViewRect, point)) {
                return false;
            }
            var curTime = new Date().getMilliseconds();
            var dwMillisecspan = curTime - _dLastTime;
            if (dwMillisecspan < 10)//暂时设定10毫秒 ，前后相应时间差必须大于10毫秒。
            {
                return false;
            }
            var cx = 0, cy = 0;
            if (point.x < docViewRect.left)
                cx = -20;
            if (point.y < docViewRect.bottom)
                cy = -20;
            if (point.x > docViewRect.right)
                cx = 20;
            if (point.y > docViewRect.top)
                cy = 20;
            pDocView.MovePage(cx, cy);
            return true;
        }
        /**
        * @description 是否存在被选择文本
        * @return {Boolean} 存在返回true,否则返回false;
        */
        this.IsSelected = function () {
            var count = _pSLArray.length;
            for (var i = 0; i < count; i++) {
                var pPage = _self.GetTextPage(_pSLArray[i]);
                if (!pPage)
                    continue;
                var rect_array = [];
                if (pPage.pageSelectInfo.GetSelectPDFRect(rect_array) != 0) {
                    return true;
                }
            }
            return false;
        };
        
        this.Select_GetPageRange = function()
        {
        	var nStartPage= _pReaderApp.GetMainView().GetDocView().GetPageCount();
        	var nEndPage = -1;
        	var size = _pSLArray.length;
        	for (var i=0; i<size; i++)
        	{
        		if (_pSLArray[i] < nStartPage)
        		{
        			nStartPage = _pSLArray[i];
        		}
        		if (_pSLArray[i] > nEndPage)
        		{
        			nEndPage = _pSLArray[i];
        		}
        	}
        	return {start:nStartPage, end:nEndPage};
        };
    };

    //静态函数
    FXREADER.TextSelect.CReader_TextSelectTool.Instance = null;
    /**
    * @description （静态方法）获取FXREADER.TextSelect.CReader_TextSelectTool的一个实例，如果之前已创建就返回之前的，否则新创建
    * @static
    * @param {FXREADER.CReaderApp} pReaderApp App实例对象
    * @param {Boolean} bUseReaderDocTextpage 是否使用文本页面
    * @return {FXREADER.TextSelect.CReader_TextSelectTool} 返回文本选择处理对象;
    */
    FXREADER.TextSelect.CReader_TextSelectTool.CreateTextSelectTool = function (pReaderApp) {
        if (FXREADER.TextSelect.CReader_TextSelectTool.Instance == null) {
            FXREADER.TextSelect.CReader_TextSelectTool.Instance = new FXREADER.TextSelect.CReader_TextSelectTool(pReaderApp);
        }
        return FXREADER.TextSelect.CReader_TextSelectTool.Instance;
    };

    /**
    * @description （静态方法）验证当前鼠标点击所在的是否在该页面
    * @static
    * @param {FXREADER.PDFView.CPDF_PageView} pPageView 页面视图对象
    * @param {object} event 鼠标事件
    * @param {FXREADER.CPDF_Point} ptOffsetCurPageView 用来返回当前鼠标位置在当前页面中的坐标
    * @return {Boolean} 在该页面区域内则返回true,否则返回false;
    */
    FXREADER.TextSelect.CReader_TextSelectTool.GetPtOffsetPageView = function (pPageView, event, ptOffsetCurPageView) {
        if (!pPageView)
            return false;
        var pageOffsetHtmlDoc = $("#" + pPageView.GetPageContainerID()).offset();
        var pageViewWidth = pPageView.GetPageViewWidth();
        var pageViewHeight = pPageView.GetPageViewHeight() + 14;
        var pageViewRect = new CPDF_Rect(0, pageOffsetHtmlDoc.top,
                              pPageView.GetDocView().GetDocViewWidth(), pageOffsetHtmlDoc.top + pageViewHeight);
        FXREADER.RectUtils.Normalize(pageViewRect);
        ptOffsetCurPageView.x = event.pageX - pageOffsetHtmlDoc.left;
        ptOffsetCurPageView.y = event.pageY - pageOffsetHtmlDoc.top;
        if (FXREADER.RectUtils.PtInRect(pageViewRect, event.pageX, event.pageY)) {
            return true;
        }
        return false;
    };
    /**
    * @description （静态方法）获取当前鼠标点击所在的页面
    * @static
    * @param {FXREADER.CReaderApp} pReaderApp 应用类实例
    * @param {object} event 鼠标事件
    * @param {FXREADER.CPDF_Point} ptOffsetCurPageView 用来返回当前鼠标位置在当前页面中的坐标
    * @param {Boolean} bGetCurPageView 为true:当鼠标点没在显示页面中时，会再查找当前页面；否则不会
    * @param {Boolean} bEnableOutofDocView 是否允许在文档视图区域之外
    * @return {FXREADER.PDFView.CPDF_PageView} 返回当前鼠标点击所在的页面对象;
    */
    FXREADER.TextSelect.CReader_TextSelectTool.GetPageViewInfoByMouseEvent = function (pReaderApp, event, ptOffsetCurPageView, bGetCurPageView, bEnableOutofDocView) {
        var pDocView = pReaderApp.GetMainView().GetDocView();
        var visablePageRange = pDocView.GetDisplayPageRange();
        var pCurClickPageView = null;
        var tmpPt = new CPDF_Point(event.pageX, event.pageY);
        var docViewRect = pDocView.GetDocViewClientRect();
        if (!bEnableOutofDocView && !FXREADER.RectUtils.PtInRect(docViewRect, tmpPt)) {
            return pCurClickPageView;
        }
        for (var i = visablePageRange.begin; i <= visablePageRange.end; i++) {
            var pPageView = pReaderApp.GetMainView().GetDocView().GetPageView(i);
            if (pPageView == null)
                continue;
            if (FXREADER.TextSelect.CReader_TextSelectTool.GetPtOffsetPageView(pPageView, event, ptOffsetCurPageView)) {
                pCurClickPageView = pPageView;
                break;
            }
        }
        if (!pCurClickPageView && bGetCurPageView) {
            var iPageIndex = pReaderApp.GetMainView().GetDocView().GetCurPageIndex();
            var pPageView = pReaderApp.GetMainView().GetDocView().GetPageView(iPageIndex);
            FXREADER.TextSelect.CReader_TextSelectTool.GetPtOffsetPageView(pPageView, event, ptOffsetCurPageView);
            pCurClickPageView = pPageView;
        }

        return pCurClickPageView;
    };

    return FXREADER.TextSelect.CReader_TextSelectTool;
});